﻿Public Class frmStatus

#Region "Declaração de variáveis"
    Private objStatus As New Controle.ctrStatus
    Private iIdStatus As Int32
    Private sMensagemValidacao As String = ""
#End Region

#Region "Propriedades"

    Public Property IDStatus() As Int32
        Get
            Return Me.iIdStatus
        End Get
        Set(ByVal value As Int32)
            Me.iIdStatus = value
        End Set
    End Property

#End Region

#Region "Métodos"

    Private Sub frmEstado_alteraModo(ByVal eModoAtual As frmPadrao.eModo) Handles Me.alteraModo
        If (eModoAtual = eModo.eAguardar Or eModoAtual = eModo.eExcluir) Then
            Me.lblDescricao.Enabled = False
            Me.txtDescricao.Enabled = False
            Me.lblPeriodicidade.Enabled = False
            Me.txtPeriodicidade.Enabled = False
            Me.lblTolerancia.Enabled = False
            Me.txtTolerancia.Enabled = False
            Me.chkTornaInativo.Enabled = False
            Me.lblCodigo.Enabled = True
            Me.txtIdStatus.Enabled = True
            Me.cmdProcura.Enabled = True
        Else
            Me.lblDescricao.Enabled = True
            Me.txtDescricao.Enabled = True
            Me.lblPeriodicidade.Enabled = True
            Me.txtPeriodicidade.Enabled = True
            Me.lblTolerancia.Enabled = True
            Me.txtTolerancia.Enabled = True
            Me.chkTornaInativo.Enabled = True
            Me.lblCodigo.Enabled = False
            Me.txtIdStatus.Enabled = False
            Me.cmdProcura.Enabled = False
        End If
    End Sub

    Private Sub frmEstado_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.txtIdStatus.Focus()
        modoAtual = eModo.eAguardar
    End Sub

    Private Sub cmdGravar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGravar.Click
        Dim bRetorno As Boolean
        Try
            If Me.validarFormulario Then
                bRetorno = Me.objStatus.salvar(Me.IDStatus, _
                                               Me.txtDescricao.Text, _
                                               Val(Me.txtPeriodicidade.Text), _
                                               Val(Me.txtTolerancia.Text), _
                                               Me.chkTornaInativo.Checked)

                If bRetorno Then
                    MyBase.Codigo = Me.objStatus.IDStatus
                    Me.IDStatus = MyBase.Codigo
                    MyBase.modoAtual = eModo.eAguardar
                    Me.txtIdStatus.Text = Me.IDStatus
                End If
            Else
                MsgBox(Me.sMensagemValidacao, MsgBoxStyle.Exclamation, Me.Text)
                Me.cmdCancelar.Text = "Cancelar"
                MyBase.modoAtual = eModo.eEditar
                Exit Sub
            End If

        Catch ex As Exception
            Me.Limpar(Me)
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub cmdExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExcluir.Click
        Try
            If MyBase.Exclui Then
                If Me.objStatus.excluir(Convert.ToInt32(Me.txtIdStatus.Text)) Then
                    MyBase.Codigo = 0
                    Me.IDStatus = 0
                    MyBase.Limpar(Me)
                    MyBase.modoAtual = eModo.eAguardar
                    Me.txtIdStatus.Focus()
                End If
            End If
        Catch ex As Exception
            MyBase.Codigo = 0
            Me.IDStatus = 0
            MyBase.modoAtual = eModo.eAguardar
            Me.Limpar(Me)
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    Private Sub cmdNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNovo.Click
        Me.txtDescricao.Focus()
    End Sub

    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        If Me.cmdCancelar.Text = "Alterar" Then
            Me.txtIdStatus.Focus()
        Else
            Me.txtDescricao.Focus()
        End If
    End Sub

    Private Function validarFormulario() As Boolean

        Dim bRetorno As Boolean = True
        sMensagemValidacao = ""

        If Me.txtDescricao.Text = String.Empty Then
            sMensagemValidacao = "- Descrição obrigatória."
            bRetorno = False
        End If

        If (chkTornaInativo.Checked = False) Then
            If Me.txtPeriodicidade.Text = String.Empty Then
                sMensagemValidacao &= Environment.NewLine & "- Periodicidade obrigatória."
                bRetorno = False
            End If

            If Me.txtTolerancia.Text = String.Empty Then
                sMensagemValidacao &= Environment.NewLine & "- Tolerância obrigatória."
                bRetorno = False
            End If
        End If

        Return bRetorno

    End Function

    Private Sub carregaDados()
        Dim drDados As DataRow

        Try
            If Me.IDStatus > 0 Then
                drDados = Me.objStatus.selecionarStatus(Me.IDStatus)
                If (Not drDados Is Nothing) Then
                    With drDados
                        Me.txtDescricao.Text = Persistencia.perConversao.nuloParaVazio(.Item("Descricao"))
                        Me.txtPeriodicidade.Text = Persistencia.perConversao.nuloParaZero(.Item("Periodicidade"))
                        Me.txtTolerancia.Text = Persistencia.perConversao.nuloParaZero(.Item("Tolerancia"))
                        Me.chkTornaInativo.Checked = Persistencia.perConversao.nuloParaBoleano(drDados.Item("TornaInativo"))
                    End With
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

#End Region

    Private Sub txtIdStatus_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIdStatus.LostFocus
        Dim drDados As DataRow

        Try
            If Me.txtIdStatus.Text <> String.Empty Then
                drDados = Me.objStatus.selecionarStatus(Convert.ToInt32(Me.txtIdStatus.Text))
                If Not drDados Is Nothing Then
                    Me.txtDescricao.Text = drDados.Item("Descricao")
                    Me.txtPeriodicidade.Text = drDados.Item("Periodicidade")
                    Me.txtTolerancia.Text = drDados.Item("Tolerancia")
                    MyBase.Codigo = drDados.Item("IDStatus")
                    Me.IDStatus = MyBase.Codigo
                    MyBase.modoAtual = eModo.eAguardar
                Else
                    MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub cmdProcura_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcura.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        sql = Me.objStatus.sqlConsulta

        With frmDialogo
            .Campos = "iIDStatus,sDescricao"
            .Descricoes = "Código,Descrição"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa de Status"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    MyBase.Codigo = .Id
                    Me.IDStatus = MyBase.Codigo
                    Me.txtIdStatus.Text = Me.IDStatus
                    Me.carregaDados()
                    Me.modoAtual = eModo.eAguardar
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If

        End With
    End Sub

    Private Sub chkTornaInativo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTornaInativo.CheckedChanged
        If (chkTornaInativo.Checked = True) Then
            txtPeriodicidade.Text = String.Empty
            txtPeriodicidade.Enabled = False
            txtTolerancia.Text = String.Empty
            txtTolerancia.Enabled = False
        Else
            txtPeriodicidade.Enabled = True
            txtTolerancia.Enabled = True
        End If
    End Sub
End Class
